package sv.com.renacempleo.web.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import sv.com.renacempleo.web.controller.objects.CheckBox;
import sv.com.renacempleo.web.util.WebUtilidades;

@Controller
@RequestMapping("/")
public class BaseController implements Serializable {

	private static final long serialVersionUID = 6283825851839217030L;
	protected static final Logger logger = Logger.getLogger(BaseController.class);
	protected static final String MESSAGE = "message";
	protected static final String SUCCESS = "success";
	protected static final String FAILURE = "failure";
	protected static final String UPDATED_DATA = "Datos Actualizados";
	protected static final String DEFAULT_ERROR_MESSAGE = WebUtilidades.DEFAULT_ERROR_MESSAGE;
	
	@RequestMapping(value = "home", method = {RequestMethod.GET, RequestMethod.POST})
	public String home(HttpServletRequest request,
			@RequestParam(required = false) String j_username) {
		if(StringUtils.isNotBlank(j_username))
			request.getSession().setAttribute("username", j_username);
		return "home";
	}
	
	protected void serializeString(String string, HttpServletResponse response){
		try {
			response.setContentType("text/plain");
			response.getOutputStream().write(string.getBytes("UTF-8"));
		} catch (Exception e) {
			logger.error("Exception: ", e);
		}
	}

	protected void serializeObject(Object object, HttpServletResponse response){
		try {
			String string = JSONObject.fromObject(object).toString();
			serializeString(string, response);
		} catch (Exception e) {
			logger.error("Exception: ", e);
		}
	}
	
	protected void serializeCollection(Object object, HttpServletResponse response){
		try {
			String string = JSONArray.fromObject(object).toString();
			serializeString(string, response);
		} catch (Exception e) {
			logger.error("Exception: ", e);
		}
	}

	@InitBinder()
	protected void initBinder(WebDataBinder binder) {
		binder.setIgnoreInvalidFields(true);
		binder.setDisallowedFields("ext*");
		binder.setFieldDefaultPrefix(StringUtils.trimToEmpty(binder.getObjectName() + "."));
	}

	protected List<CheckBox> createCheckboxGroup(List<Object[]> list, String name){
		CheckBox checkbox = null;
		List<CheckBox> listToReturn = new ArrayList<CheckBox>(); 
		for (Object[] object : list) {
			checkbox = new CheckBox();
			checkbox.setBoxLabel((String) object[1]);
			checkbox.setInputValue((Long) object[0]);
			checkbox.setName(name);
			checkbox.setChecked(object[2] != null);
			listToReturn.add(checkbox);
		}
		return listToReturn;
	}
	
	protected List<CheckBox> createCheckboxGroup(Object[][] list, String name, List<Object[]> selecteds){
		CheckBox checkbox = null;
		List<CheckBox> listToReturn = new ArrayList<CheckBox>(); 
		for (Object[] object : list) {
			checkbox = new CheckBox();
			checkbox.setBoxLabel((String) object[1]);
			checkbox.setInputValue(new Long((String) object[0]));
			checkbox.setName(name);
			checkbox.setChecked(selecteds.contains(object[0]));
			listToReturn.add(checkbox);
		}
		return listToReturn;
	}
}
